package pkg.chemdrm;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

public class AIPlayer extends Player
{
	public final static int AI_DIR_DOWN = 0;
	public final static int AI_DIR_UP = 2;
	public final static int AI_DIR_RIGHT = 4;
	public final static int AI_DIR_LEFT = 4;
	
	public final static int STATE_AGGRESSIVE = 0;
	public final static int STATE_PASSIVE = 1;
	
	public final float mPlayerVelocity = 96f;
	
	public final static float CHANGE_TIME = 1f;
	
	public AIRegion mRegion;
	
	private int mAnimatedState = AI_DIR_DOWN;
	
	private int mRegionId;
	private int mUniqueId;
	private int mState = STATE_PASSIVE;
	
	private float mLifetime = 0;
	private float mLastUpdate = 0;
	
	public String mType;
	
	public AIPlayer(AIRegion pRegion, float pX, float pY, float pWidth, float pHeight, TiledTextureRegion pTextureRegion, String pType) 
	{
		super(pX, pY, pWidth, pHeight, pTextureRegion);
		mRegion = pRegion;
		setCurrentTileIndex(0);
		mType = pType;
	}
	
	public AIPlayer(AIRegion pRegion, float pX, float pY, float pWidth, float pHeight, TiledTextureRegion pTextureRegion, String pType, ResourceHandler pRM) 
	{
		super(pX, pY, pWidth, pHeight, pTextureRegion);
		mRegion = pRegion;
		setCurrentTileIndex(0);
		
		mResourceHandler = pRM;
		mType = pType;
	}
	
	public void setId(int pId)
	{
		mRegionId = mUniqueId = pId;
	}
	
	public void destroySelf()
	{
		detachSelf();
		
	}
	
	public void goAggressive()
	{
		mState = STATE_AGGRESSIVE;
	}
	
	public void goPassive()
	{
		mState = STATE_PASSIVE;
	}
	
	public void stopMoving()
	{
		mPlayerXVelocity = 0;
		mPlayerYVelocity = 0;
	}

	
	public void update(float dt)
	{
		mLifetime += dt;
		
	//	System.err.println("Hey Shanee from robot " + mUniqueId + " I'm at " + mX + ", " + mY);
		float tX = mX;
		float tY = mY;
		
		mX += mPlayerXVelocity*dt;
		mY += mPlayerYVelocity*dt;
		if(mX >= mRegion.getX() + mRegion.getWidth() || mX <= mRegion.getX() || mY >= mRegion.getY() + mRegion.getHeight() || mY <= mRegion.getY())
		{
			//System.err.println("Trying to leave region: " + mX + ", " + mY);
			stopMoving();
			mX = tX;
			mY = tY;
		}
		
		
	//	if(!this.collidesWith(mRegion))
	//		stopMoving();
		

		
		switch(mState)
		{
		case STATE_AGGRESSIVE:
			
			
			break;
			
		case STATE_PASSIVE:
			
			if(Math.abs(mLastUpdate - mLifetime) >= CHANGE_TIME)
			{
				//if(mUniqueId == 0)
				//	System.err.println("TOP: Tile is " + this.getCurrentTileIndex() + " Animated state is " + mAnimatedState);
			
				
				mLastUpdate = mLifetime;
			
				int dir = (int)(Math.random()*3.99); // pick a new random direction and GO.
				
			
				
				
				switch(dir)
				{
				case 0: // up
					stopMoving();
					setFlippedHorizontal(false);
					mAnimatedState = AI_DIR_UP;
					setMove(0, -1);

					
					break;
				case 1: // down
					stopMoving();
					setFlippedHorizontal(false);
					mAnimatedState = AI_DIR_DOWN;
					setMove(0, 1);
	
	
					break;
				case 2: // left
					stopMoving();
					setFlippedHorizontal(true);
					mAnimatedState = AI_DIR_RIGHT;
					setMove(-1, 0);
	
					
					break; 
				case 3: // right
					stopMoving();
					setFlippedHorizontal(false);
					mAnimatedState = AI_DIR_RIGHT;
					setMove(1, 0);
	
					break;
					
				default:
					System.err.println("AIPlayer::Impossible direction!!!");
				}
				//if(mUniqueId == 0)
				//	System.err.println("BOTTOM: Tile is " + this.getCurrentTileIndex() + " Animated state is " + mAnimatedState);
				
				
				
			}
				break;
			
				default: System.err.println("PlayerAI::lol, wut?");
			
		}
	}
	
	public void setMove(int pX, int pY)
	{
		// hopefully whoever is passing this in uses it correctly. Use the static ints defined at the top of Player.
		//this.stopAnimation(mAnimatedState);
		long[] blah = new long[2];
		blah[0] = 100;
		blah[1] = 100;
		
		this.animate(blah, mAnimatedState, mAnimatedState+1, true);
		
		/* This locks the size of pX and pY to 1. */
		if(Math.abs(pX) > 1)
			pX /= Math.abs(pX);
		if(Math.abs(pY) > 1)
			pY /= Math.abs(pY);
		
		if(Math.abs(pX) > 0 && Math.abs(pY) > 0)
		{
			mPlayerXVelocity = (float) (mPlayerVelocity * 0.7071)*pX;
			mPlayerYVelocity = (float) (mPlayerVelocity * 0.7071)*pY;
		}
		else
		{
			mPlayerXVelocity = (float) (mPlayerVelocity)*pX;
			mPlayerYVelocity = (float) (mPlayerVelocity)*pY;
		}
		
		// hopefully whoever is passing this in uses it correctly. Use the static ints defined at the top of Player.
		//this.setCurrentTileIndex(dir);

	}
	
	public boolean isInRegion(Sprite pSprite)
	{
		return mRegion.collidesWith(pSprite);
		
	}
	
	
	public int getId()
	{
		return mUniqueId;
	}
	
	public int getRegionId()
	{
		return mRegionId;
	}
 
}
